package com.szmirren.entity;

import com.alibaba.fastjson.JSONObject;
import com.szmirren.models.TableAttributeKeyValue;

import java.util.HashMap;
import java.util.Map;

/**
 * SqlAssist的实体类
 * 
 * @author <a href="http://szmirren.com">Mirren</a>
 *
 */
public class MapperContent {
	/** SqlAssist类的包名 */
	private String classPackage;
	/** SqlAssist类的类型 */
	private String className;
	/** SqlAssist类的配置文件 */
	private Map<String, Object> item = new HashMap<>();

	private String resultMapName;

	/**
	 * 初始化
	 */
	public MapperContent() {
		super();
	}

	/**
	 * 通过包名与类名初始化
	 * 
	 * @param classPackage
	 * @param className
	 */
	public MapperContent(String classPackage, String className) {
		super();
		this.classPackage = classPackage;
		this.className = className;
	}
	
	
	
	
	public String getClassPackage() {
		return classPackage;
	}

	public void setClassPackage(String classPackage) {
		this.classPackage = classPackage;
	}

	public String getClassName() {
		return className;
	}

	public void setClassName(String className) {
		this.className = className;
	}

	public String getResultMapName() {
		return resultMapName;
	}

	public void setResultMapName(String resultMapName) {
		this.resultMapName = resultMapName;
	}

	public Map<String, Object> getItem() {
		return item;
	}

	public void setItem(Map<String, Object> item) {
		String dataValue = "";
		try{
			for (Map.Entry<String, Object> m :item.entrySet()) {
				if (m.getKey().equals("resultMap")){
					Object object = m.getValue();
					String data = JSONObject.toJSONString(object);
					TableAttributeKeyValue tableAttributeKeyValue = JSONObject.parseObject(data, TableAttributeKeyValue.class);
					String value = tableAttributeKeyValue.getValue();
					if (value.contains(".")){
						dataValue = value.substring(0,value.lastIndexOf("."));
					}
					dataValue = dataValue.replaceAll("_","");
				}
			}
		}catch (Exception e){
			e.printStackTrace();
		}
		setResultMapName(dataValue);
		this.resultMapName = dataValue;
		this.item = item;
	}

	@Override
	public String toString() {
		return "SQLContent [classPackage=" + classPackage + ", className=" + className + ", item=" + item + "]";
	}

}
